package org.voidness.oje2d;

import java.util.Vector;

import org.lwjgl.opengl.GL11;

/** Text console class (output only) */
public class GLTextConsole {
    /** The lines in the console */
    private Vector<Line> lines = null;

    /** The maximum number of lines allowed */
    private int maxLines;

    /** The font to use for drawing */
    private GLFont font = null;

    /** The color white */
    private GLColor white = null;

    /** A black transparent color */
    private GLColor transparentBlack = null;

    /**
     * Default constructor for the text console.
     * 
     * @param mFont The font to use
     * @param mMaxLines The maximum number of lines allowed
     */
    public GLTextConsole(GLFont mFont, int mMaxLines) {
        lines = new Vector<Line>();
        font = mFont;
        maxLines = mMaxLines;
        white = GLColor.WHITE;
        transparentBlack = new GLColor(0.0f, 0.0f, 0.0f, 0.3f);
    }

    /**
     * Adds a new text line to the console.
     * 
     * @param mText The text to add
     * @param mColor The color to use for that text
     */
    public void addLine(String mText, GLColor mColor) {
        // If we are on the limit we erase the first
        // before we insert the new
        if (lines.size() == maxLines)
            lines.removeElementAt(0);
        lines.add(new Line(mText, mColor));
    }

    /**
     * Removes a text line from the console.
     * 
     * @param mIndex The index of the line to remove
     */
    public void removeLineAt(int mIndex) {
        if (mIndex > -1 && mIndex < lines.size())
            lines.removeElementAt(mIndex);
    }

    /**
     * Draws the console at mX, mY.
     * 
     * @param mNumber The number of lines to draw
     * @param mX The X position where to draw
     * @param mY The Y position where to draw
     * @param mZoom The zoom factor of the text
     * @param mBoxOn True to draw a background box
     */
    public void draw(int mNumber, int mX, int mY, float mZoom, boolean mBoxOn) {
        white.set();

        // Just to make sure it doesnt crash
        if (mNumber > lines.size())
            mNumber = lines.size();

        font.setZoom(mZoom);

        if (mBoxOn) {
            // Trying to find out what is the size of the biggest string from
            // the ones we will draw
            int width = 0;
            int count = 0;
            for (int i = lines.size() - mNumber; i < lines.size(); i++) {
                Line line = (Line) lines.elementAt(i);
                for (int j = 0; j < line.string.length(); j++)
                    count++;
                if (count > width / font.getCharSize())
                    width = count * font.getCharSize();
                count = 0;
            }

            // Now drawing the box and text
            GL11.glEnable(GL11.GL_BLEND);
            Engine.drawQuad(mX, mY - font.getCharSize(), width, mNumber
                    * font.getCharSize(), transparentBlack);
            GL11.glDisable(GL11.GL_BLEND);
        }

        // E agora desenhamos o texto
        for (int i = lines.size() - mNumber; i < lines.size(); i++) {
            Line line = (Line) lines.elementAt(i);
            font.print(line.string, mX, mY, mZoom, line.color);
            mY += 10;
        }

        white.set();
    }

    /**
     * Get the number of lines in the console.
     * 
     * @return The number of lines
     */
    public int getSize() {
        return lines.size();
    }
}

/** A class to represent a console text line. */
class Line {
    /** The text for this line */
    public String string;

    /** The color used by this line */
    public GLColor color;

    /**
     * Default constructor for a text line.
     * 
     * @param mText The text to save
     * @param mColor The color to use when drawing the text
     */
    public Line(String mText, GLColor mColor) {
        string = mText;
        color = mColor;
    }
}